<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <el-form
          class="login_form"
          :model="formData"
          :rules="rules"
          ref="loginForm"
        >
          <h1>Hello</h1>
          <h2>欢迎来到运营平台</h2>
          <el-form-item prop="username">
            <el-input
              v-model="formData.username"
              :prefix-icon="User"
              size="large"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model="formData.password"
              show-password
              :prefix-icon="Lock"
              size="large"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="login-btn"
              size="large"
              @click="login"
              :loading="loading"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { User, Lock } from '@element-plus/icons-vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'
let $router = useRouter()
let $route = useRoute()
let useStore = useUserStore()
const formData = reactive({
  username: 'admin',
  password: 'atguigu123',
})
const loading = ref(false)
const loginForm = ref()
const validatorUserName = (rules: any, value: any, callback: any) => {
  if (value.length >= 5) {
    callback()
  } else {
    callback(new Error('用户名长度至少5位'))
  }
}
const validatorPassword = (rules: any, value: any, callback: any) => {
  if (value.length >= 6) {
    callback()
  } else {
    callback(new Error('密码长度至少6位'))
  }
}
const rules = ref({
  username: [
    { required: true, message: '用户名不能为空', trigger: 'blur' },
    { validator: validatorUserName, trigger: 'blur' },
    // {min:5,max:8,message:'用户名长度在5-8位',trigger:'blur'}
  ],
  password: [
    { required: true, message: '密码不能为空', trigger: 'blur' },
    { validator: validatorPassword, trigger: 'blur' },
    // {min:6,max:15,message:'密码长度在6-15位',trigger:'change'}
  ],
})
// 登录按钮
const login = async () => {
  await loginForm.value.validate()

  loading.value = true
  try {
    await useStore.useLogin(formData)
    // 判断路径是否带有query参数
    let redirect: any = $route.query.redirect
    $router.push({ path: redirect || '/' })
    loading.value = false
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `hi ,${getTime()}`,
    })
  } catch (error) {
    loading.value = false
    ElNotification({
      type: 'error',
      message: (error as Error).message,
    })
  }
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;
  .login_form {
    position: relative;
    top: 30vh;
    width: 80%;
    // background: url('@/assets/images/login_form') no-repeat;
    // background-size: cover;
    background: #305abf;
    padding: 40px;
    h1 {
      color: #fff;
      font-size: 40px;
    }
    h2 {
      color: #fff;
      font-size: 34px;
      margin: 20px 0px;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
